﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Apache.NMS;
using Apache.NMS.ActiveMQ;
using Apache.NMS.Util;
using Apache.NMS.ActiveMQ.Commands;
using System.Collections;
using System.IO;

namespace Transport.AMQ
{
    class MyQueue
    {
        private static IConnection _connection;
        private static ISession _session;
        private static IDestination _destination;
        private static IQueueBrowser _browser;
        private static IConnectionFactory _factory;
        private static ArrayList _messagequeue = new ArrayList();

        public static ArrayList MessageQueue
        {
            get { return _messagequeue; }
        }

        public const String QUEUE_ADVISORY_DESTINATION = "parameters";

        public MyQueue(){
            _factory = new ConnectionFactory();
            _connection = _factory.CreateConnection();
            _connection.Start();
            _session = _connection.CreateSession();
            _destination = _session.GetQueue(QUEUE_ADVISORY_DESTINATION);
            _browser = _session.CreateBrowser((IQueue)_destination);
        }

        public void RetrieveMessages()
        {
            IEnumerator enumeration = _browser.GetEnumerator();
            ArrayList values = new ArrayList();
           // Logger log = Logger.getInstance();
            while (enumeration.MoveNext())
            {
                IMessage message = (IMessage)enumeration.Current;
                values = (ArrayList)message.Properties.Values;
                _messagequeue.Add(values);
                Console.WriteLine("Value written: " + values[0].ToString() + "#" + values[values.Count - (values.Count - 3)].ToString());
                //log.WriteToFile();
            }
            //log.Close();
        }
    }
}
